package com.xl.controller;

import com.xl.event.AlarmCreateEvent;
import com.xl.event.AlarmDeleteEvent;
import com.xl.event.AlarmIgnoreEvent;
import com.xl.event.AlarmInfoSource;
import com.xl.event.publish.AlarmEventPublisher;
import com.xl.pojo.resp.AlarmResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/alarm")
public class WarnController {

//    @Autowired
//    private AlarmEventPublisher publisher ;

//    @Autowired
//    private AlarmEventPublisherAware publisherAware ;

    //底层直接通过系统的事件发布者 去发布事件
    @Autowired
    private AlarmEventPublisher publisher;


    @GetMapping("/create")
    public AlarmResp createAlarm() {
        System.out.println("产生一个告警....");

        //发布事件
        AlarmInfoSource info = new AlarmInfoSource();
        info.setDeviceId(100l);
        info.setMessageObj("告警一");
        AlarmCreateEvent alarmCreateEvent = new AlarmCreateEvent(info);
        publisher.publish(alarmCreateEvent);

        //返回
        AlarmResp<String> alarmResp = new AlarmResp<>();
        alarmResp.success("产生一个告警");
        return alarmResp;
    }

    @GetMapping("/ignore")
    public AlarmResp ignoreAlarm() {
        System.out.println("忽略一个告警...");

        //发布事件
        AlarmInfoSource info = new AlarmInfoSource();
        info.setDeviceId(222l);
        info.setMessageObj("告警二");
        AlarmIgnoreEvent alarmIgnoreEvent = new AlarmIgnoreEvent(info);
        publisher.publish(alarmIgnoreEvent);
        //返回
        AlarmResp<String> alarmResp = new AlarmResp<>();
        alarmResp.success("忽略一个告警");
        return alarmResp;
    }

    @GetMapping("/delete")
    public AlarmResp deleteAlarm() {
        System.out.println("删除一个告警...");
        //发布事件
        AlarmInfoSource info = new AlarmInfoSource();
        info.setDeviceId(333l);
        info.setMessageObj("删除一个告警");
        AlarmDeleteEvent alarmDeleteEvent = new AlarmDeleteEvent(info);
        publisher.publish(alarmDeleteEvent);
        //返回
        AlarmResp<String> alarmResp = new AlarmResp<>();
        alarmResp.success("删除一个告警");
        return alarmResp;
    }

}
